﻿
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model IEnumerable<YTCMS.Domain.Entities.category>

<div class="row">
    <div class="col-xs-12">
        <!-- PAGE CONTENT BEGINS -->
        <div class="panel panel-default" >
            <div class="panel-heading">
                <h2><a class="btn btn-sm btn-warning" href="/category/add"><i class="fa fa-plus"></i> 新增</a></h2>
                <div class="panel-actions">
                    <a href="table.html#" class="btn-setting"><i class="fa fa-rotate-right"></i></a>
                    <a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
                    <a href="table.html#" class="btn-close"><i class="fa fa-times"></i></a>
                </div>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dd" id="nestable">
                            <ol class="dd-list">
                                <li class="dd-item" data-id="1">
                                    <div class="dd-handle" value="0" level="0">
                                        默认分类
                                        <div class="pull-right action-buttons">
                                            <a class="blue" href="javascript:void(0)" name="a_add" value="0" level="0">
                                                <i class="icon-plus-sign bigger-130"></i>
                                            </a>
                                        </div>
                                    </div>
                                    <ol class="dd-list">
                                        @foreach (var item in Model.Where(s => s.Level == 1 && s.Pid == 0))
                                        {
                                            <li class="dd-item" data-id="2">
                                                <div class="dd-handle" value="@item.Id" level="1">
                                                    @item.Name
                                                    <div class="pull-right action-buttons">
                                                        <a class="blue" href="javascript:void(0)" name="a_add" value="@item.Id" level="1">
                                                            <i class="icon-plus-sign bigger-130 "></i>
                                                        </a>
                                                        <a class="pink" href="javascript:void(0)" name="a_edit" value="@item.Id" level="1">
                                                            <i class="icon-pencil bigger-130  "></i>
                                                        </a>
                                                        <a class="red" href="javascript:void(0)" name="a_delete" value="@item.Id" level="1">
                                                            <i class="icon-trash bigger-130"></i>
                                                        </a>
                                                    </div>
                                                </div>
                                                <ol class="dd-list">
                                                    @foreach (var item2 in Model.Where(s => s.Level == 2 && s.Pid == item.Id))
                                                    {
                                                        <li class="dd-item" data-id="3">
                                                            <div class="dd-handle" value="@item2.Id" level="2">
                                                                @item2.Name
                                                                <div class="pull-right action-buttons">
                                                                    <a class="blue" href="javascript:void(0)" name="a_add" value="@item2.Id" level="2">
                                                                        <i class="icon-plus-sign bigger-130 "></i>
                                                                    </a>
                                                                    <a class="pink" href="javascript:void(0)" name="a_edit" value="@item2.Id" level="2">
                                                                        <i class="icon-pencil bigger-130  "></i>
                                                                    </a>
                                                                    <a class="red" href="javascript:void(0)" name="a_delete" value="@item2.Id" level="2">
                                                                        <i class="icon-trash bigger-130"></i>
                                                                    </a>
                                                                </div>
                                                            </div>
                                                            <ol class="dd-list">
                                                                @foreach (var item3 in Model.Where(s => s.Level == 3 && s.Pid == item2.Id))
                                                                {
                                                                    <li class="dd-item" data-id="4">
                                                                        <div class="dd-handle" value="@item3.Id" level="3">
                                                                            @item3.Name
                                                                            <div class="pull-right action-buttons">
                                                                                <a class="blue" href="javascript:void(0)" name="a_add" value="@item3.Id" level="3">
                                                                                    <i class="icon-plus-sign bigger-130 "></i>
                                                                                </a>
                                                                                <a class="pink" href="javascript:void(0)" name="a_edit" value="@item3.Id" level="3">
                                                                                    <i class="icon-pencil bigger-130  "></i>
                                                                                </a>
                                                                                <a class="red" href="javascript:void(0)" name="a_delete" value="@item3.Id" level="3">
                                                                                    <i class="icon-trash bigger-130"></i>
                                                                                </a>
                                                                            </div>
                                                                        </div>

                                                                    </li>
                                                                }
                                                            </ol>
                                                        </li>
                                                    }
                                                </ol>

                                            </li>
                                        }
                                    </ol>
                                </li>
                            </ol>

                            
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h2><i class="fa fa-indent red"></i><strong>默认分类</strong></h2>
                            </div>
                            <div class="panel-body">
                                <form action="" method="post" id="form1">
                                    <div class="form-group">
                                        <label for="nf-email">分类名称</label>
                                        <input type="text" id="txt_categoryname" name="categoryname" class="form-control" placeholder="分类名称">
                                    </div>
                                    <div class="form-group">
                                        <label for="nf-email">所属父级</label>
                                        <input type="text" id="txt_parent" name="parent" class="form-control disabled" placeholder="所属父级" readonly>
                                    </div>
                                    <div class="form-group">
                                        <label for="nf-password">状态</label>
                                        @Html.DropDownList("ddl_State", Admin.Common.YTDDL.CommonState(true), new { @class = "form-control" })
                                    </div>
                                    @Html.Hidden("hidpid")
                                    @Html.Hidden("Level")
                                    @Html.Hidden("hidid")
                                </form>
                            </div>
                            <div class="panel-footer">
                                <button type="submit" class="btn btn-sm btn-success" id="btn_save"><i class="fa fa-dot-circle-o"></i> 保存</button>
                                <button type="reset" class="btn btn-sm btn-danger" onclick="javascript:document.form1.reset();"><i class="fa fa-ban"></i> 重置</button>
                            </div>
                        </div>
                    </div>
                </div><!-- PAGE CONTENT ENDS -->
            </div>
        </div>
        
    </div><!-- /.col -->
</div><!-- /.row -->

<script type="text/javascript">
    jQuery(function ($) {
        $('.dd').nestable();
        $('.dd-handle a').on('mousedown', function (e) {
            e.stopPropagation();
        });
        $('[data-rel="tooltip"]').tooltip();
        
        $("a[name='a_add']").click(function () {
            var that = $(this);
            var pname = that.parent().parent(".dd-handle").text();
            var pid = that.attr("value");
            var plevel = that.attr("level")
            $("#hidpid").val(pid);
            $("#Level").val(parseInt(plevel) + 1);
            $("#txt_parent").val($.trim(pname))
        });
        $("#btn_save").click(function () {
            $.post("/Category/Add", $("#form1").serialize(), function (result) {
                if (result.d) {
                    jAlert("操作成功", "提示", function () {
                        window.location.href = window.location.href;
                    })
                }
                else {
                    jAlert(result.msg, "提示");
                }
            })
        });

        $("a[name='a_edit']").click(function () {
            var that = $(this);
            var id = that.attr("value");
            var pname = that.parent().parent(".dd-handle").parent().parent().prev("div").text();
            var pid = that.parent().parent(".dd-handle").parent().parent().prev("div").attr("value");
            var plevel = that.parent().parent(".dd-handle").parent().parent().prev("div").attr("level")
            var name = that.parent().parent(".dd-handle").text();
            $("#hidpid").val(pid);
            $("#Level").val(parseInt(plevel) + 1);
            $("#txt_parent").val($.trim(pname))
            $("#txt_categoryname").val($.trim(name))
            $("#hidid").val(id)
            $("#btn_save").click(function () {
                $.post("/Category/Add", $("#form1").serialize(), function (result) {
                    if (result.d) {
                        jAlert("操作成功", "提示", function () {
                            window.location.href = window.location.href;
                        })
                    }
                    else {
                        jAlert(result.msg, "提示");
                    }
                })
            });
        });
        $("a[name='a_delete']").click(function () {
            var that = $(this);
            jConfirm("您确定要执行删除吗？", "提示", function (b) {
                if (b)
                {
                    var id = that.attr("value");
                    $.post("/category/delete", { "id": id }, function (result) {
                        if (result.d) {
                            jAlert("操作成功", "提示", function () { window.location.href = window.location.href })
                        }
                        else {
                            jAlert(result.msg, "提示")
                        }
                    })
                }
            })
        });
    });
</script>